package com.zkingsoft.actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.plugin.util.HttpUtils;
import com.zkingsoft.plugin.util.PropertiesUtils;
import com.zkingsoft.plugin.util.RandomValidateCode;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @通用控制器,本action未经session过验证器
 */
@Controller
@RequestMapping(value = "/common")
public class CommonController extends BaseController {

	/**
	 * @页面定向方法
	 */
	@RequestMapping(value = "/redirect/{page1}/{page2}")
	public String redirect(@PathVariable("page1") String page1, @PathVariable("page2") String page2) {
		return "common/" + page1 + "/" + page2;
	}

	/**
	 * @页面定向方法
	 */
	@RequestMapping(value = "/redirect/{page1}")
	public String redirect(@PathVariable("page1") String page1) {
		return "common/" + page1;
	}

	/**
	 * 
	 * @向页面输出一个验证码，并存放在session中
	 * @session取验证码名称RandomValidateCode.RANDOMCODEKEY
	 */
	@RequestMapping(value = "/validateImg")
	public void messagelistiIn(HttpServletRequest request, HttpServletResponse response) {
		RandomValidateCode.newValidateImg(request, response);
	}
	
	/**
	 * @用户退出系统
	 */
	@RequestMapping(value = "/loginOut")
	public String loginOut(HttpSession session, HttpServletResponse response) throws Exception {
		session.invalidate();
		return "redirect:/common/redirect/login";
	}

	/**
	 * @登录验证
	 */
	@RequestMapping(value = "/loginValue")
	public @ResponseBody Object loginValue(HttpServletRequest request, String userName, String userPass) throws IOException {
		// String conditions[]={"acct_no","pass_word"};
		// String conditionsValue[]={acct_no,pass_word};
		// Map<String, Object> map = service.getMapByConditions("*",
		// "sys_shopstaff_info", conditions, conditionsValue);
		//
		//
		// if(acct_no==null){
		// acct_no=request.getSession().getAttribute("acct_no").toString();
		// if(acct_no!=null){
		// return new ModelAndView("admin/index");
		// }
		// }
		//
		// if(map!=null && !map.isEmpty()){
		// request.getSession().setAttribute("acct_no", acct_no);
		// request.getSession().setAttribute("pass_word", pass_word);
		// return new ModelAndView("admin/index");
		// }else {
		// request.getSession().setAttribute("message", "用户名密码错误");
		// return new ModelAndView("login/login");
		// }
		Map<String, Object> map = new HashMap<String, Object>();
		//测试配置文件配置接口地址
		try {
			String postUrl = PropertiesUtils.get("config/JSONInterface", "login");
			//String postUrl = "http://127.0.0.1:8080/airCondition-web/system/loginCheck";//请求接口
			//String postData = "userName=admin&password=123";//传递数据
			List<NameValuePair> postData = new ArrayList<NameValuePair>();  
			postData.add(new BasicNameValuePair("userName", userName));
			postData.add(new BasicNameValuePair("userPass", userPass));
			
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONObject json = JSONObject.fromObject(body);
			
			/*
			 * 将返回的body数据进行设置
			 */
			// request.getSession().setAttribute("userName", userName);
			// request.getSession().setAttribute("userPass", userPass);
			//return new ModelAndView("/admin/welcome");
			
			if((int)json.get("resultCode") == 0) {
				map.put("url", "common/redirect/exhibition");
				map.put("status", "OK");
				String user = String.valueOf(json.get("data"));
//			 	String roleId = "";
				request.getSession().setAttribute("roleId", "");
				if(!"null".equals(user)) {
					JSONObject userObj = JSONObject.fromObject(user);
					if(userObj.get("roleId") != null) {
						request.getSession().setAttribute("roleId", userObj.get("roleId"));
					}
				}
			} else {
				map.put("status", "FAIL");
			}
		}catch(Exception e) {
			map.put("status", "ERROR");
			map.put("url", "common/redirect/login");
		}
		return map;
	}
	
	/**
	 * @获取首页数据
	 */
	@RequestMapping(value = "/commonIndex")
	public @ResponseBody Object commonIndex(HttpServletRequest request, String livedata) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String postUrl = PropertiesUtils.get("config/JSONInterface", "commonIndex");
		//String postUrl = "http://127.0.0.1:8080/airCondition-web/system/loginCheck";//请求接口
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();  
		postData.add(new BasicNameValuePair("livedata", livedata));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONObject json = JSONObject.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @获取首页年统计数据
	 */
	@RequestMapping(value = "/commonIndexDevYearCount")
	public @ResponseBody Object commonIndexDevYearCount(HttpServletRequest request) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String postUrl = PropertiesUtils.get("config/JSONInterface", "commonIndexDevYearCount");
		//String postUrl = "http://127.0.0.1:8080/airCondition-web/system/loginCheck";//请求接口
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONArray json = JSONArray.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @获取首页月统计数据
	 */
	@RequestMapping(value = "/commonIndexDevMonthCount")
	public @ResponseBody Object commonIndexDevMonthCount(HttpServletRequest request) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String postUrl = PropertiesUtils.get("config/JSONInterface", "commonIndexDevMonthCount");
		//String postUrl = "http://127.0.0.1:8080/airCondition-web/system/loginCheck";//请求接口
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONArray json = JSONArray.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @获取首页运维统计数据
	 */
	@RequestMapping(value = "/commonIndextotal")
	public @ResponseBody Object commonIndextotal(HttpServletRequest request) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String postUrl = PropertiesUtils.get("config/JSONInterface", "commonIndextotal");
		//String postUrl = "http://127.0.0.1:8080/airCondition-web/system/loginCheck";//请求接口
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONObject json = JSONObject.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @获取首页维修人员列表数据
	 */
	@RequestMapping(value = "/commonIndexServerList")
	public @ResponseBody Object commonIndexServerList(HttpServletRequest request) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String postUrl = PropertiesUtils.get("config/JSONInterface", "commonIndexServerList");
		//String postUrl = "http://127.0.0.1:8080/airCondition-web/system/loginCheck";//请求接口
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			JSONArray json = JSONArray.fromObject(body);
			map.put("data", json);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
	
	/**
	 * @首页维修人员选派
	 */
	@RequestMapping(value = "/maintainPost")
	public @ResponseBody Object maintainPost(HttpServletRequest request, String serialNum, String userId) throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String postUrl = PropertiesUtils.get("config/JSONInterface", "maintainPost");
		//String postUrl = "http://127.0.0.1:8080/airCondition-web/system/loginCheck";//请求接口
		//String postData = "userName=admin&password=123";//传递数据
		List<NameValuePair> postData = new ArrayList<NameValuePair>();
		postData.add(new BasicNameValuePair("serialNum", serialNum));
		postData.add(new BasicNameValuePair("userId", userId));
		
		try {
			String body = HttpUtils.post(postUrl, postData);//调用请求方法，并返回请求后的数据
			//JSONObject json = JSONObject.fromObject(body);
			map.put("data", body);
			map.put("status", "OK");
		} catch(Exception e) {
			map.put("status", "FAIL");
		}
		return map;
	}
}